package com.huichaoliu.adapters;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.huichaoliu.app.R;
import com.huichaoliu.data.Config;
import com.rederxu.AsyncTask.ImageTask;
import com.rederxu.request.HTTPRequest;
import com.rederxu.request.onRequestOverListener;
import com.source.utils.Utils;

import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class ProductAdapter extends BaseAdapter {

	private Context mContext = null;
	private JSONArray ja = null;

	public ProductAdapter(Context mContext, JSONArray ja) {
		this.mContext = mContext;
		this.ja = ja;
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return ja.length();
	}

	public String getCode(int position) {
		try {
			return ja.getJSONObject(position).getString("commodityId");
		} catch (JSONException e) {
			return null;
		}
	}

	public String getCover(int position) {
		try {
			return ja.getJSONObject(position).getString("image");
		} catch (JSONException e) {
			return null;
		}
	}

	public String getName(int position) {
		try {
			return ja.getJSONObject(position).getString("name");
		} catch (JSONException e) {
			return null;
		}
	}

	public String getUrl(int position) {
		try {
			return ja.getJSONObject(position).getString("url");
		} catch (JSONException e) {
			return null;
		}
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		ViewHolder holder = null;
		if (convertView == null) {
			convertView = LayoutInflater.from(mContext).inflate(
					R.layout.item_good, null);
			holder = new ViewHolder();
			holder.init(convertView);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		try {
			JSONObject jo = ja.getJSONObject(position);
			holder.tv_name.setText(jo.getString("name"));
			holder.tv_like.setText("共有" + jo.getInt("praisenumber") + "位用户点赞");
			holder.tv_price.setText("￥" + jo.getString("price"));

			ImageTask it = new ImageTask(holder.ic_cover,
					Config.getFolderImages(), null);
			it.execute(jo.getString("image"));

			boolean ispraise = jo.optBoolean("ispraise", false);
			if (ispraise) {
				holder.ic_like.setTag("");
				((ImageView) holder.ic_like)
						.setImageResource(R.drawable.ic_like_selected);
			} else {
				holder.ic_like.setTag(null);
				((ImageView) holder.ic_like)
						.setImageResource(R.drawable.ic_like_normal);
			}

			holder.ic_collect.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					if (!Config.isOnline(mContext)) {
						return;
					}
					if (v.getTag() == null) {
						v.setTag("");
						((ImageView) v)
								.setImageResource(R.drawable.ic_collect_selected);
					} else {
						v.setTag(null);
						((ImageView) v)
								.setImageResource(R.drawable.ic_collect_normal);
					}
				}
			});

			holder.ic_like.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					if (!Config.isOnline(mContext)) {
						return;
					}
					try {
						if (ja.getJSONObject(position).getBoolean("ispraise")) {
							like(false, position);
							int nowLike = ja.getJSONObject(position).getInt("praisenumber") - 1;
							nowLike = nowLike < 0 ? 0 : nowLike;
							ja.getJSONObject(position).put("praisenumber", nowLike);
							ja.getJSONObject(position).put("ispraise", false);
							notifyDataSetChanged();
						} else {
							like(true, position);
							ja.getJSONObject(position).put("praisenumber", ja.getJSONObject(position).getInt("praisenumber") + 1);
							ja.getJSONObject(position).put("ispraise", true);
							notifyDataSetChanged();
						}
					} catch (Exception e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			});
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return convertView;
	}

	static class ViewHolder {
		private ImageView ic_cover;
		private ImageView ic_like;
		private ImageView ic_collect;
		private TextView tv_price;
		private TextView tv_name;
		private TextView tv_like;

		private void init(View view) {
			ic_cover = (ImageView) view.findViewById(R.id.ic_cover);
			ic_like = (ImageView) view.findViewById(R.id.ic_like);
			ic_collect = (ImageView) view.findViewById(R.id.ic_collect);
			tv_price = (TextView) view.findViewById(R.id.tv_price);
			tv_name = (TextView) view.findViewById(R.id.tv_name);
			tv_like = (TextView) view.findViewById(R.id.tv_like);
		}
	}

	private void like(boolean isAdd, int position) {
		try {
			JSONObject jo = new JSONObject();
			jo.put("userId", Config.getUserId(mContext));
			jo.put("commodityId", getCode(position));
			jo.put("isadd", isAdd);
			Bundle bundle = new Bundle();
			bundle.putString("request", Utils.requestParamsEncode(jo));
			HTTPRequest.POST(mContext, Config.REQUEST_URLS.URL_SHOPPING_LIKE,
					bundle, new onRequestOverListener() {
						@Override
						public void onRequestOver(String result) {
							try {
								JSONObject jo = new JSONObject(result);
								if (jo.getInt("code") != 0) {
									Toast.makeText(mContext,
											jo.getString("message"), 0).show();
									return;
								}
							} catch (JSONException e) {
								e.printStackTrace();
							}
						}
					});
		} catch (Exception e) {

		}
	}
}
